import { makeAutoObservable, observable } from 'mobx';
import * as api from './api';

export default class CustomerStore {
  _args = {
    search: {
    },
    pg: {
      page: 0,
      sort: ['crDate;desc'],
      size: 10,
    },
  };
  total = 0;
  customers = [];
  coupons = [];
  activities = [];
  loading = false;
  _tabStatus = '1';

  constructor(rootStore) {
    this.rootStore = rootStore;
    makeAutoObservable(this, { rootStore: false });
  }

  /**
   * 使用设置的请求参数和分页配置请求列表数据
   * 分页参数为空时保持当前分页配置
   * @param {object} pgInfo 分页参数
   */
  * query(pgInfo) {
    _.mergeWith(this._args.pg, pgInfo, (o, s) => (_.isArray(o) ? s : undefined)); // 数组直接赋值，不合并
    const { search, pg } = toJS(this._args);
    if (!pg.sort?.length) delete pg.sort;
    this.loading = true;
    try {
      this.total = yield api.count({ search });
      const res = yield api.get({ search, pg });
      this.customers = yield Promise.all(res.map(async (p) => {
        const manager = await api.getManagersInfo({ search: { targetUserUsername: p?.username }, pg: { page: 0, size: 1 } });
        return { ...p, receiverName: manager.length === 0 ? '' : manager[0].receiverName };
      }));
    } finally {
      this.loading = false;
    }
  }

  * get(id) {
    const res = yield api.get(id);
    return observable(res);
  }

  * update(customer) {
    yield api.update(customer);
    yield this.query();
  }

  * deleteUser(customer) {
    yield api.deleteCustomer(customer.username);
    yield this.query();
  }

  * getCoupons(search) {
    const res = yield api.getCoupons(search);
    this.coupons = res.map((item) => ({
      label: `${item.name}(${item.id})`,
      value: item.id,
    }));
  }

  * getActivities(search) {
    const res = yield api.getActivities(search);
    this.activities = res.map((item) => ({
      label: `${item.name}(${item.id})`,
      value: item.id,
    }));
  }

  * getManagersInfo(search) {
    return yield api.getManagersInfo(search);
  }

  * getManagersInfoCount(search) {
    return yield api.getManagersInfoCount(search);
  }

  idx(i) {
    return this._args.pg.page * this._args.pg.size + i + 1;
  }

  get pg() {
    return this._args.pg;
  }

  set pg(pg) {
    this.query({ ...pg });
  }

  get search() {
    this._args.pg.page = 0;
    return this._args.search;
  }

  set search(search) {
    this._args.pg.page = 0;
    this._args.search = search;
    this.query({ page: 0 });
  }

  get tableSort() {
    if (this._args.pg.sort?.length) {
      const [fn, dir] = this._args.pg.sort[0].split(';');
      return { [fn]: dir };
    }
    return undefined;
  }

  get api() {
    return api;
  }

  /**
   * @description 获取用户身份类型
   * @param {*} id 字典名称
   * @returns 数组
   */
  * getType(id) {
    const typeList = yield api.getType(id);
    const selectData = typeList.map((item) => {
      return ({
        label: item.name,
        value: item.itemId,
      });
    });
    return selectData;
  }

  /**
   * @description 获取指定用户
   * @param {*} id
   * @returns 对象
   */
  * getUser(id) {
    try {
      const user = yield api.getCustomerUser(id);
      return user;
    } catch (error) {
      console.log(error);
    }
  }

  get tab_Status() {
    return this._tabStatus;
  }

  set tab_Status(value) {
    this._tabStatus = value;
  }
}
